import * as vscode from 'vscode';

export interface FileWatcherCallback {
    (eventType: string, filePath: string): void;
}

export class FileWatcher {
    private watcher: vscode.FileSystemWatcher;
    private callback: FileWatcherCallback;

    constructor(callback: FileWatcherCallback) {
        this.callback = callback;
        
        // 创建文件系统监视器
        this.watcher = vscode.workspace.createFileSystemWatcher('**/*', false, false, false);
        
        // 监听文件创建
        this.watcher.onDidCreate(uri => {
            this.callback('create', uri.fsPath);
        });

        // 监听文件修改
        this.watcher.onDidChange(uri => {
            this.callback('change', uri.fsPath);
        });

        // 监听文件删除
        this.watcher.onDidDelete(uri => {
            this.callback('delete', uri.fsPath);
        });

        // 监听文件保存
        vscode.workspace.onDidSaveTextDocument(doc => {
            this.callback('save', doc.uri.fsPath);
        });
    }

    public dispose(): void {
        this.watcher.dispose();
    }
}
